import CryptoJS from "crypto-js"; //AES CBC模式
// 加密
export function encrypt(word: string, keyStr: string, ivStr: string) {
	if (!word) return;
	keyStr = keyStr ? keyStr : "absoietlj32fai12";
	ivStr = ivStr ? ivStr : "absoietlj32fai12";
	let key = CryptoJS.enc.Utf8.parse(keyStr);
	let iv = CryptoJS.enc.Utf8.parse(ivStr);
	let srcs = CryptoJS.enc.Utf8.parse(word);

	let encrypted = CryptoJS.AES.encrypt(srcs, key, {
		iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.toString();
}
// 解密
export function decrypt(word: string, keyStr: string, ivStr: string) {
	if (!word) return;
	keyStr = keyStr ? keyStr : "absoietlj32fai12";
	ivStr = ivStr ? ivStr : "absoietlj32fai12";
	let key = CryptoJS.enc.Utf8.parse(keyStr);
	let iv = CryptoJS.enc.Utf8.parse(ivStr);

	let decrypt = CryptoJS.AES.decrypt(word, key, {
		iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return decrypt.toString(CryptoJS.enc.Utf8);
}
